package com.letv.whatslive.model.convert;

import com.letv.whatslive.model.Vote;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by Administrator on 2016/6/7.
 */
public class VoteConvert {
    private static final String DEFAULT_PIC = "http://g3.letv.cn/vod/v1/MTk3LzQ3LzU2L2xldHYtaXR2Mi8wL3N0YXRpY2ZpbGUvaW1ncy8yMDE2MDYyMS83MGRjM2NjMDkzNjEyNzg5ODMzOTFiOTJjOTQ1YjIyOC5qcGc=?b=123456&platid=5&splatid=500";

    public static DBObject castVoteToDBObject(Vote vote) {
        DBObject dbo = new BasicDBObject();
        if (vote.getId() != null) {
            dbo.put("_id", vote.getId());
        }
        dbo.put("voteNum", ObjUtils.toLong(vote.getVoteNum(), 0L));
        dbo.put("programId", vote.getProgramId());
        dbo.put("pName", ObjUtils.toString(vote.getPName(), ""));
        dbo.put("programPic", ObjUtils.toString(vote.getProgramPic(), ""));
        dbo.put("userId", vote.getUserId());
        dbo.put("nickName", ObjUtils.toString(vote.getNickName(), ""));
        dbo.put("userPic", ObjUtils.toString(vote.getUserPic(), ""));
        dbo.put("createTime", ObjUtils.toLong(vote.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", ObjUtils.toLong(vote.getUpdateTime(), System.currentTimeMillis()));
        if(vote.getActId()!=null){
            dbo.put("actId", ObjUtils.toLong(vote.getActId()));
        }

        return dbo;
    }

    public static Vote castDBObjectToVote(DBObject dbObject) {
        Vote vote = new Vote();
        if (dbObject != null) {
            vote.setId(ObjUtils.toLong(dbObject.get("_id")));
            vote.setVoteNum(ObjUtils.toLong(dbObject.get("voteNum")));
            vote.setProgramId(ObjUtils.toLong(dbObject.get("programId")));
            vote.setPName(ObjUtils.toString(dbObject.get("pName")));
            vote.setProgramPic(ObjUtils.toString(dbObject.get("programPic") == "" ? DEFAULT_PIC : dbObject.get("programPic"), DEFAULT_PIC));
            vote.setUserId(ObjUtils.toLong(dbObject.get("userId")));
            vote.setNickName(ObjUtils.toString(dbObject.get("nickName")));
            String picture = ObjUtils.toString(dbObject.get("userPic"));
            if (picture != null && !"".equals(picture)) {
                String[] pictures = picture.split(",");
                vote.setUserPic(pictures[0]);
            }
            vote.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            vote.setUpdateTime(ObjUtils.toLong(dbObject.get("updateTime")));
            vote.setActId(ObjUtils.toLong(dbObject.get("actId")));

        } else {
            return null;
        }

        return vote;
    }

}
